package com.cat.graphTheory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-the-number-of-complete-components/
 * @create 2025/9/18 22:31
 * @since JDK17
 */

public class Solution08 {
    List<Set<Integer>> g = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    boolean[] vis;
    void dfs(int cur) {
        vis[cur] = true;
        list.add(cur);
        for (int x : g.get(cur)) {
            if (!vis[x]) {
                dfs(x);
            }
        }
    }
    boolean judge() {
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (!g.get(list.get(i)).contains(list.get(j))) {
                    return false;
                }
            }
        }
        return true;
    }
    public int countCompleteComponents(int n, int[][] edges) {
        for (int i = 0; i < n; i++) {
            g.add(new HashSet<>());
        }
        vis = new boolean[n];
        int m = edges.length, ans = 0;
        for (int i = 0; i < m; i++) {   // 建图
            int from = edges[i][0], to = edges[i][1];
            g.get(from).add(to);
            g.get(to).add(from);
        }
        for (int i = 0; i < n; i++) {
            list.clear();
            if (!vis[i]) {  //
                dfs(i);
                if (judge()) {
                    ans++;
                }
            }
        }

        return ans;
    }
}
